var dbConfig = require('../utils/dbconfig');

//携带参数的sql
checkLogin = (req,res)=>{
    console.log('接到请求');
    //获取请求参数
    console.log(req)
    let {username,password} = req.body;
    var sql = "select count(1) isTrue from user_info where user_id=? and password=?";
    var sqlArr = [username,password];
    var callBack = (err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            console.log(username+"===="+password)
            res.send({
                'code':data[0].isTrue == '1'?200:300,
                'token':'Bearer 59ae1342-2851-416a-a025-0fe755c48f1c'
            })
        }
    }

    dbConfig.sqlConnect(sql,sqlArr,callBack);
}

createToken =


























// 添加验证码校验
validPhoneCode = [];
let sendCodeP = (phone)=>{
    console.log('========================================');
    console.log(validPhoneCode);
    for(var item of validPhoneCode){
        console.log(item);
        console.log('item==>'+item.phone)
        if(phone == item.phone){
            return true
        }
    }
    return false
}

//验证手机号和验证码
let findCodeAndPhone = (phone,code)=>{
    for(var item of validPhoneCode){
        if(phone == item.phone && code == item.code){
            return 'login'
        }
    }
    return 'error'
}

// 使用body-parse处理post请求
sendCode=(req,res)=>{
    let phone = req.query.phone;
    // 验证电话是否发送过验证码
    if(sendCodeP(phone)){

    }else{
        let code = '1234'
        validPhoneCode.push({
            'phone':phone,
            'code':code
        })
    }

    console.log(validPhoneCode)
    res.send({
        'code':200,
        'msg':'发送成功',
        'phone':phone
    })
}

// 验证码登陆
codePhoneLogin = (req,res)=>{
    let {phone,code} = req.query;
    //验证是否发过验证码
    if(sendCodeP(phone)){
        //匹配验证码和手机号
        let status = findCodeAndPhone(phone,code);
        if(status == 'login'){
            res.send({
                'code':200,
                'msg':'登陆成功'
            })
        }else if(status == 'error'){
            res.send({
                'code':200,
                'msg':'登陆失败'
            })
        }
    }else{
        res.send({
            'code':400,
            'msg':'未发送失验证码'
        })
    }
}

module.exports = {
    checkLogin,checkLogin,sendCode,codePhoneLogin
}
